﻿using BDPAutodetect.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BDPAutodetect.Client.ViewWindows.WindowDialog
{
    /// <summary>
    /// SettingUserEditorDialog.xaml 的交互逻辑
    /// </summary>
    [ObservableObject]
    public partial class SettingUserEditorDialog : Window, IDialog<MUserDto>
    {
        /// <summary>
        /// 是否选择显示明文密码
        /// </summary>
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(IsVisibilityPassword))]
        protected bool isCheckedPassword = false;
        /// <summary>
        /// 显示标题
        /// </summary>
        [ObservableProperty]
        protected string titleName = "编辑";
        /// <summary>
        /// 显示状态信息
        /// </summary>
        [ObservableProperty]
        protected string statusMsg = string.Empty;
        /// <summary>
        /// 当前编辑参数界限数据
        /// </summary>
        [ObservableProperty]
        protected MUserDto editorData;
        /// <summary>
        /// 角色选项集
        /// </summary>
        [ObservableProperty]
        protected ObservableCollection<MRoleDto> roleItems;

        /// <summary>
        /// 源数据
        /// </summary>
        protected MUserDto? SourceData { get; set; }
        /// <summary>
        /// 返回结果信息
        /// </summary>
        public MUserDto? DialogResultData { get; set; }
        /// <summary>
        /// 是否显示明文密码 
        /// </summary>
        public Visibility IsVisibilityPassword => this.IsCheckedPassword ? Visibility.Visible : Visibility.Hidden;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SettingUserEditorDialog(string argTitleName, MUserDto? argUserData, ObservableCollection<MRoleDto> argRoles)
        {
            InitializeComponent();
            this.TitleName = argTitleName;
            this.RoleItems = argRoles;
            this.EditorData = argUserData != null ? argUserData.CloneDeep() : new MUserDto();
            this.SourceData = argUserData;
            this.DataContext = this;
        }

        /// <summary>
        /// 确认选择事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmOk()
        {
            if (!Check()) return;

            this.DialogResult = true;
            this.DialogResultData = this.EditorData;
            this.DialogResultData.Name = this.DialogResultData.Name.Trim();
            if (!string.IsNullOrWhiteSpace(this.DialogResultData.NameNick))
            {
                this.DialogResultData.NameNick = this.DialogResultData.NameNick.Trim();
            }
            this.Close();
        }

        /// <summary>
        /// 取消事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmCancel()
        {
            this.DialogResult = false;
            this.Close();
        }

        /// <summary>
        /// 检查
        /// </summary>
        /// <returns></returns>
        public bool Check()
        {
            if (this.EditorData.Role == null)
            {
                this.StatusMsg = "角色不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.EditorData.Name))
            {
                this.StatusMsg = "名称不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.EditorData.NameNick))
            {
                this.StatusMsg = "别称不能为空";
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.EditorData.PasswordDisplay))
            {
                this.StatusMsg = "密码不能为空";
                return false;
            }

            if (this.SourceData == null) return true;
            if (this.EditorData.Role != this.SourceData.Role)
            {
                return true;
            }
            if (this.EditorData.Name.Equals(this.SourceData.Name, StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            if (this.EditorData.NameNick.Equals(this.SourceData.NameNick, StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            if (this.EditorData.PasswordDisplay.Equals(this.SourceData.PasswordDisplay))
            {
                return true;
            }

            this.StatusMsg = "未调整，可以直接点击取消";
            return false;
        }
    }
}
